 //
// Copyright (c) 2003-2009, MindFusion LLC - Bulgaria.
//

using System;
using System.Drawing;

чё за хня?

using MindFusion.Diagramming;
using MindFusion.Drawing;
using Pen = System.Drawing.Pen;
using SolidBrush = System.Drawing.SolidBrush;


namespace SN
{
    /// <summary>
    /// Summary description for IconNode.
    /// </summary>
    public class CIconNode : ShapeNode
    {
        private Color _backColor;

        public Color BackColor
        {
            get
            {
                return Color.FromArgb(_backColor.R, _backColor.G, _backColor.B);
            }
            set
            {
                _backColor = Color.FromArgb(value.R, value.G, value.B);
                Brush = new MindFusion.Drawing.SolidBrush(Color.Transparent);

            }
        }
        //public Image Picture{ set; get;}

        private float _oldSize;

        public float NewSize
        {
            get
            {
                var newSize = Bounds.Width != _oldSize ? Bounds.Width : Bounds.Height;
                _oldSize = newSize;
                return newSize;
            }
        }



        static CIconNode()
        {
            DefaultIcon = new Bitmap(48, 48);

            var graphics = Graphics.FromImage(DefaultIcon);
            var font = new Font("Arial", 48, FontStyle.Bold, GraphicsUnit.Pixel);

            graphics.FillRectangle(Brushes.Transparent, 0, 0, 48, 48);
            graphics.DrawString("?", font, Brushes.Black, 0, 0);

            font.Dispose();
            graphics.Dispose();
        }

        public CIconNode(Diagram diagram)
            : base(diagram)
        {
            _icon = DefaultIcon;
            _label = "";
            _format = new StringFormat { Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Center };
            Bounds = new RectangleF(Bounds.Location, CalculateSize());

            ImageAlign = ImageAlign.Fit;
            BackColor = Color.Green;
            Brush = new MindFusion.Drawing.SolidBrush(Color.Transparent);
            Pen=new MindFusion.Drawing.Pen(Color.Transparent,0);

        }


        public override void Draw(IGraphics graphics, RenderOptions options)
        {
            if(Image!=null)
                base.Draw(graphics, options);
            _label = (string)Tag;
            foreach (DiagramLink link in GetAllLinks())
                link.Text = link.Tag.ToString();

            var min = Math.Min(Bounds.Height, Bounds.Width);
            // Draw the icon center at the top
            if(Image==null)
                graphics.FillEllipse(new SolidBrush(BackColor), Bounds.X, Bounds.Y, min, min);

            // Draw the label at the bottom
            var labelBounds = RectangleF.FromLTRB(Bounds.X, Bounds.Y + Bounds.Height, Bounds.Right, Bounds.Bottom);

            foreach (DiagramLink link in OutgoingLinks)
            {
                if (link.Selected)
                {
                    //var linkMin = Math.Min(link.Bounds.Height, link.Bounds.Width);
                    //graphics.DrawEllipse(new Pen(Color.White, 3), link.Bounds.X, link.Bounds.Y, linkMin, linkMin);

                    //graphics.DrawLine(new Pen(Color.White, 3), link.Bounds. + link.Origin.Bounds.Width / 2, link.Origin.Bounds.Y + link.Origin.Bounds.Height / 2, link.Destination.Bounds.X + link.Destination.Bounds.Width / 2, link.Destination.Bounds.Y + link.Destination.Bounds.Height / 2);

                    graphics.DrawLine(new Pen(Color.White, 3), link.ControlPoints[0].X, link.ControlPoints[0].Y,
                                      link.ControlPoints[1].X, link.ControlPoints[1].Y);
                }

            }

            if (Selected && Image==null)
                graphics.DrawEllipse(new Pen(Color.White, 3), Bounds.X, Bounds.Y, min, min);
            if (Selected && Image != null)
                graphics.DrawRectangle(new Pen(Color.White, 3), Bounds.X, Bounds.Y, min, min);

            graphics.DrawString(_label, Font, Brushes.Black, labelBounds, _format);

        }

        public override void DrawShadow(IGraphics graphics, RenderOptions options)
        {
        }

        private SizeF CalculateSize()
        {
            var tempImage = new Bitmap(1, 1);
            var graphics = Graphics.FromImage(tempImage);
            var measureGraphics = new GdiGraphics(graphics) { PageUnit = Parent.MeasureUnit };

            var iconSizePixels = new Rectangle(0, 0, _icon.Width, _icon.Height);
            var imageSize = MindFusion.Utilities.DeviceToDoc(
                measureGraphics, iconSizePixels);

            measureGraphics.Dispose();
            tempImage.Dispose();

            var textSize = Parent.MeasureString(_label, Font, int.MaxValue, _format);

            return new SizeF(
                Math.Max(imageSize.Width, textSize.Width),
                imageSize.Height + textSize.Height);
        }

        protected override void UpdateCreate(PointF current)
        {
            base.UpdateCreate(current);
            Bounds = new RectangleF(current, CalculateSize());
        }

        protected override void SaveTo(System.IO.BinaryWriter writer, PersistContext ctx)
        {
            base.SaveTo(writer, ctx);

            // Save the label using the standard .NET BinaryWriter
            writer.Write(_label);

            // Save the image using the MindFusion.Diagramming built-in image saving code,
            // which stores the contents of shared images only once.
            ctx.SaveImage(_icon);
        }

        protected override void LoadFrom(System.IO.BinaryReader reader, PersistContext ctx)
        {
            base.LoadFrom(reader, ctx);

            _label = reader.ReadString();
            _icon = ctx.LoadImage();
        }


        public Image Icon
        {
            get { return _icon; }
            set
            {
                _icon = value;
                Bounds = new RectangleF(Bounds.Location, CalculateSize());
            }
        }

        public string Label
        {
            get { return _label; }
            set
            {
                _label = value;
                Bounds = new RectangleF(Bounds.Location, CalculateSize());
            }
        }


        private Image _icon;
        private string _label;
        private readonly StringFormat _format;

        static private readonly Image DefaultIcon;
    }
}